home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / motif / olight.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  8.9 KB  |  314 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*----------------------------------------------------------------------------
  18.  *
  19.  * olight.c : openGL (motif) example showing how to do hardware lighting
  20.  *            including two_sided lighting.
  21.  *
  22.  * Author : Yusuf Attarwala
  23.  *          SGI - Applications
  24.  * Date   : Mar 93
  25.  *
  26.  *    press  left   button for animation
  27.  *           middle button for two sided lighting (default)
  28.  *           right  button for single sided lighting
  29.  *
  30.  *
  31.  *---------------------------------------------------------------------------*/
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35.  
  36. #include <Xm/Xm.h> 
  37. #include <Xm/Frame.h>               /* for frame widgets */
  38. #include <Xm/Form.h>               /* for frame widgets */
  39. #include <X11/keysym.h>             /* keyboard translations */
  40. #include <X11/StringDefs.h>
  41.  
  42. #include <GL/gl.h>                  /* gl includes */
  43. #include <GL/glu.h>                 /* utility library includes */
  44. #include <GL/GLwMDrawA.h>           /* include for the drawing area widget */
  45.  
  46. /* function declarations */
  47.  
  48. void 
  49.     createToplevel(void),
  50.     drawScene(void),
  51.     setMatrix(void),
  52.     initLightAndMaterial(void),
  53.     animation(void),
  54.     exposeCB(Widget,XtPointer,XtPointer),
  55.     resizeCB(Widget,XtPointer,XtPointer),
  56.     initCB(Widget,XtPointer,XtPointer),
  57.     inputCB(Widget,XtPointer,XtPointer);
  58.  
  59.  
  60. /* global variables */
  61.             
  62. Display       *display;             /* current display */
  63. XtAppContext  appContext;           /* X application context */
  64. float         ax,ay,az;             /* angles for animation */
  65. GLUquadricObj *quadObj;             /* used in drawscene */
  66. static float lmodel_twoside[] = { GL_TRUE };
  67. static float lmodel_oneside[] = { GL_FALSE };
  68.  
  69. Widget        toplevel,       /* toplevel shell */
  70.               glw;            /* current widget */
  71.              
  72. GLXContext    glxc;           /* current glx context */
  73.  
  74. Arg args[20];
  75. int acnt;
  76.  
  77. void 
  78. main(int argc, char** argv)
  79. {
  80.     XtToolkitInitialize();
  81.     appContext = XtCreateApplicationContext();
  82.     display    = XtOpenDisplay(appContext, NULL, "Olight","olight",NULL,0,
  83.                               &argc,argv);
  84.     if (!display) {
  85.         printf("%s : Unable to open display\n",argv[0]);
  86.         exit(0);
  87.     }
  88.  
  89.     printf("\n---------------------------------------------\n");
  90.     printf("OpenGL hardware lighting example \n\n");
  91.     printf("Press:  left   button for animation\n\
  92.         middle button for two sided light\n\
  93.         right  button for single sided light\n");
  94.  
  95.     quadObj = gluNewQuadric ();   /* this will be used in drawScene */
  96.     createToplevel();             /* create widget hierarchy */
  97.     XtAppMainLoop(appContext);    /* loop forever */
  98. }
  99.  
  100.  
  101. void
  102. createToplevel(void)
  103. {
  104.     Widget frame;
  105.  
  106.     acnt = 0;
  107.     XtSetArg(args[acnt],XmNminHeight, 300);acnt++;
  108.     XtSetArg(args[acnt],XmNminWidth,  300);acnt++;
  109.     XtSetArg(args[acnt],XmNminAspectX,  1);acnt++;
  110.     XtSetArg(args[acnt],XmNminAspectY,  1);acnt++;
  111.     XtSetArg(args[acnt],XmNmaxAspectX,  1);acnt++;
  112.     XtSetArg(args[acnt],XmNmaxAspectY,  1);acnt++;
  113.     toplevel  = XtAppCreateShell("openGL light","olight",
  114.                                   applicationShellWidgetClass,
  115.                                   display,args,acnt);
  116.  
  117.     /* create a frame to hold glx widget */
  118.     frame   = XtVaCreateManagedWidget("frame", 
  119.                   xmFrameWidgetClass, toplevel, 
  120.                   NULL);
  121.  
  122.     /* create a double buffer widget, in rgb mode and manage it */
  123.     acnt = 0;
  124.     XtSetArg(args[acnt], GLwNrgba,               TRUE); acnt++;
  125.     XtSetArg(args[acnt], GLwNdoublebuffer,       TRUE); acnt++;
  126.     XtSetArg(args[acnt], GLwNallocateBackground, TRUE); acnt++;
  127.     glw = GLwCreateMDrawingArea(frame, "glw", args, acnt);
  128.     XtManageChild(glw);
  129.  
  130.     /* register callbacks */
  131.     XtAddCallback(glw, GLwNginitCallback,  initCB,   NULL);
  132.  
  133.     /* realize widget hierarchy */
  134.     XtRealizeWidget(toplevel);
  135.  
  136.     initLightAndMaterial();
  137.  
  138.     /* additional callbacks */
  139.     XtAddCallback(glw, GLwNexposeCallback, exposeCB, NULL);
  140.     XtAddCallback(glw, GLwNresizeCallback, resizeCB, NULL);
  141.     XtAddCallback(glw, GLwNinputCallback,  inputCB,  NULL);
  142.  
  143. }
  144.  
  145. void
  146. drawScene(void)
  147. {
  148.     glClearColor(0.0, 0.0, 0.0, 0.0);
  149.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  150.  
  151.     glPushMatrix();
  152.     gluQuadricDrawStyle (quadObj, GLU_FILL);
  153.     glColor3f (1.0, 1.0, 0.0);
  154.     glRotatef (ax,1.0,0.0,0.0);
  155.     glRotatef (-ay,0.0, 1.0, 0.0);
  156.  
  157.     gluCylinder (quadObj, 2.0,5.0,10.0,20,8);  /* draw a cone */
  158.  
  159.     glPopMatrix();
  160.  
  161.     glFlush();
  162.     glXSwapBuffers(XtDisplay(glw), XtWindow(glw));
  163.  
  164. }
  165.  
  166. void
  167. setMatrix(void)
  168. {
  169.     glMatrixMode(GL_PROJECTION);
  170.     glLoadIdentity();
  171.     glOrtho(-15.0,15.0,-15.0,15.0,-10.0,10.0);
  172.     glMatrixMode(GL_MODELVIEW);
  173.     glLoadIdentity();
  174. }
  175.  
  176. void
  177. animation(void)
  178. {
  179.     register int i;
  180.     /* animate the cone */
  181.  
  182.     for (i=0;i<60;i++) {
  183.         ax += 5.0;
  184.         ay -= 2.0;
  185.         az += 5.0;
  186.         if (ax >= 360)  ax = 0.0;
  187.         if (ay <= -360) ay = 0.0;
  188.         if (az >= 360)  az = 0.0;
  189.         drawScene();
  190.     }
  191. }
  192.  
  193. void 
  194. inputCB(Widget w, XtPointer client_data, XtPointer call)
  195. {
  196.     char            string[31];
  197.     XComposeStatus  composeStatus;
  198.     KeySym keysym;
  199.     GLwDrawingAreaCallbackStruct *call_data;
  200.  
  201.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  202.  
  203.     switch(call_data->event->type) {
  204.     case ButtonPress:
  205.         switch (call_data->event->xbutton.button) {
  206.         case Button1:
  207.         animation();
  208.             break;
  209.         case Button2 :
  210.             glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  211.         drawScene();
  212.             break;
  213.         case Button3 :
  214.             glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_oneside);
  215.         drawScene();
  216.             break;
  217.         }
  218.         break;
  219.     case KeyPress :
  220.         XLookupString((XKeyEvent *)call_data->event,string,
  221.                       30,&keysym,&composeStatus);
  222.         switch(keysym) {
  223.         case XK_Escape :
  224.             exit(0);
  225.             break;
  226.         }
  227.     break;
  228.     default:
  229.         break;
  230.     }
  231. }
  232.  
  233. void 
  234. resizeCB(Widget w, XtPointer client_data, XtPointer call)
  235. {
  236.     GLwDrawingAreaCallbackStruct *call_data;
  237.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  238.  
  239.  
  240.     GLwDrawingAreaMakeCurrent(w, glxc);
  241.     glViewport(0, 0, call_data->width, call_data->height);
  242.     setMatrix();
  243.     drawScene();
  244. }
  245.  
  246. void 
  247. exposeCB(Widget w, XtPointer client_data, XtPointer call)
  248. {
  249.     GLwDrawingAreaCallbackStruct *call_data;
  250.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  251.  
  252.  
  253.     GLwDrawingAreaMakeCurrent(w, glxc);
  254.     glViewport(0, 0, call_data->width, call_data->height);
  255.     drawScene();
  256. }
  257.  
  258. void
  259. initCB(Widget w, XtPointer client_data, XtPointer call)
  260. {
  261.     XVisualInfo *vi;
  262.  
  263.     XtSetArg(args[0], GLwNvisualInfo, &vi);
  264.     XtGetValues(w, args, 1);
  265.  
  266.     glxc = glXCreateContext(XtDisplay(w), vi, 0, GL_TRUE);
  267.  
  268.     ax = 10.0;
  269.     ay = -10.0;
  270.     az = 0.0;
  271. }
  272.  
  273. void 
  274. initLightAndMaterial(void)
  275. {
  276.     static float ambient[] = { 0.1, 0.1, 0.1, 1.0 };
  277.     static float diffuse[] = { 0.5, 1.0, 1.0, 1.0 };
  278.     static float position[] = { 90.0, 90.0, 150.0, 0.0 };
  279.  
  280.     static float front_mat_shininess[] = { 60.0 };
  281.     static float front_mat_specular[] = { 0.2, 0.2, 0.2, 1.0 };
  282.     static float front_mat_diffuse[] = { 0.5, 0.5, 0.28, 1.0 };
  283.     static float back_mat_shininess[] = { 60.0 };
  284.     static float back_mat_specular[] = { 0.5, 0.5, 0.2, 1.0 };
  285.     static float back_mat_diffuse[] = { 1.0, 0.9, 0.2, 1.0 };
  286.  
  287.     static float lmodel_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
  288.  
  289.     GLwDrawingAreaMakeCurrent(glw, glxc);
  290.  
  291.     setMatrix();
  292.     glEnable(GL_DEPTH_TEST);
  293.     glDepthFunc(GL_LEQUAL);
  294.  
  295.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  296.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  297.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  298.  
  299.     glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess);
  300.     glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular);
  301.     glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  302.     glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess);
  303.     glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular);
  304.     glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  305.  
  306.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  307.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  308.  
  309.     glEnable(GL_LIGHTING);
  310.     glEnable(GL_LIGHT0);
  311.     glShadeModel(GL_SMOOTH);
  312. }
  313.  
  314.